package API.Class.File;
import java.io.*;
import java.text.SimpleDateFormat;

import API.Class.util.StringPro;

import java.util.Date;

public class FileInformation {
    public static String version;

    private String cast(long l) {
        int i = 0;
        if (l >= 1024) {// KB
            i++;
            l = l / 1024;
            if (l >= 1024) {// MB
                i++;
                l = l / 1024;
                if (l >= 1024) {// GB
                    i++;
                    l = l / 1024;
                    if (l >= 1024) {// TB
                        i++;
                        l = l / 1024;
                        if (l >= 1024) {// PB
                            i++;
                            l = l / 1024;
                            if (l >= 1024) {// EB
                                i++;
                                l = l / 1024;
                                if (l >= 1024) {// ZB
                                    i++;
                                    l = l / 1024;
                                    if (l >= 1024) {// YB
                                        i++;
                                        l = l / 1024;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (i == 1) {

            return l + "KB";
        } else if (i == 2) {

            return l + "MB";
        } else if (i == 3) {

            return l + "GB";
        } else if (i == 4) {

            return l + "TB";
        } else if (i == 5) {

            return l + "PB";
        } else if (i == 6) {

            return l + "EB";
        } else if (i == 7) {

            return l + "ZB";
        } else if (i == 8) {

            return l + "YB";
        }

        return l + "B";
    }

    public String search(String filepath) {
        StringPro str = new StringPro();
        File f = new File(filepath);
        str.appendLn("文件绝对路径：" + "\"" + f.getAbsolutePath() + "\"");

        str.appendLn("文件名：" + f.getName());

        str.appendLn("文件是否存在：" + f.exists());

        long ti = f.lastModified();
        Date date = new Date(ti);
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        if (f.exists()) {
            str.appendLn("文件是否被隐藏：" + f.isHidden());

            str.appendLn("文件是否可读：" + f.canRead());

            str.appendLn("文件是否可写：" + f.canWrite());

            long l = f.length();
            FileInformation r = new FileInformation();
            str.appendLn(r.cast(l));
            str.appendLn("文件最后修改时间：" + sdf.format(date));

        } else {
            str.appendLn("文件信息读取失败，请检查文件是否可以访问且存在此文件!");
        }
        return str.toString();
    }

    public String read(String filepath) throws IOException {
        StringPro str = new StringPro();
        File f = new File(filepath);
        FileInputStream f1 = null;
        BufferedInputStream bu = null;
        if (f.exists()) {
            f1 = new FileInputStream(f);
            bu = new BufferedInputStream(f1);
            byte[] b = new byte[102400];// 缓存区
            int length = bu.read(b);
            str.append("--------------------------[文件内容]--------------------------\n" + new String(b, 0, length));
            str.appendLn("\n--------------------------------------------------------------\n");
        } else {
            str.appendLn("文件信息读取失败，请检查文件是否可以访问且存在此文件!");
        }
        return str.toString();

    }

    /*
    filepath:文件路径
    isRemWrite:是否继续往下写
    nextLine:是否换行
     */
    public void write(String filepath, String jea, boolean isRemWrite, boolean nextLine) throws IOException {
        StringPro str = new StringPro();
        File f = new File(filepath);
        FileOutputStream f1 = null;
        BufferedOutputStream bu = null;
        f1 = new FileOutputStream(f, isRemWrite);
        bu = new BufferedOutputStream(f1);
        if (nextLine) {
            String huanhang = System.getProperty("line.separator");
            bu.write(huanhang.getBytes());
            bu.flush();
        }
        bu.write(jea.getBytes(), 0, jea.getBytes().length);
        bu.flush();
    }
}
